package com.bms.api.bussiness.member.web;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.bms.api.base.annotation.LoginMemId;
import com.bms.common.base.common.bean.Result;
import com.bms.common.base.web.BaseController;
import com.bms.common.bussiness.member.entity.MemberInfo;
import com.bms.common.bussiness.member.service.IMemberInfoService;
import com.feilong.core.bean.ConvertUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

@RestController
@RequestMapping("member")
@Api(value="Member-Api",tags="会员接口")
@CacheConfig(cacheNames = "memberInfo")
public class MemberController extends BaseController{
	
	@Autowired
	private IMemberInfoService memberInfoService;

	@GetMapping("infoList.auth")
	@ApiOperation(value = "用户信息卡列表")
	@Cacheable
	public Result infoList(@ApiIgnore @LoginMemId String openId){
		return json(memberInfoService.selectByMap(ConvertUtil.toMap("open_id",openId)));
	}
	
	
	@PostMapping("save.auth")
	@ApiOperation(value = "新增信息卡")
	@CacheEvict(key="\"MemberController.infoList_\"+#openId")
	public Result save(@ApiIgnore @LoginMemId String openId,@Validated @RequestBody MemberInfo memberInfo){
		Date now = new Date();
		memberInfo.setCreateTime(now);
		memberInfo.setUpdateTime(now);
		memberInfo.setOpenId(openId);
		memberInfoService.insert(memberInfo);
		return success();
	}
	
	
	@PutMapping("update.auth")
	@ApiOperation(value = "修改信息卡")
	@CacheEvict(key="\"MemberController.infoList_\"+#openId")
	public Result update(@ApiIgnore @LoginMemId String openId,@Validated @RequestBody MemberInfo memberInfo){
		Date now = new Date();
		memberInfo.setUpdateTime(now);
		memberInfoService.updateById(memberInfo);
		return success();
	}
	
	@DeleteMapping("delete.auth")
	@ApiOperation(value = "删除信息卡")
	@CacheEvict(key="\"MemberController.infoList_\"+#openId")
	public Result delete(@ApiIgnore @LoginMemId String openId,@RequestParam Long memInfoId){
		memberInfoService.delete(new EntityWrapper<MemberInfo>().eq("open_id", openId).eq("id", memInfoId));
		return success();
	}
}
